package com.juyu.miliao.scheduler.message;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.juyu.miliao.common.constant.BillDetailBeansMqKey;
import com.juyu.miliao.common.domain.BillDetailBeans;
import com.juyu.miliao.common.domain.dto.BillDetailBeansDTO;
import com.juyu.miliao.scheduler.dao.mysql.BillDetailBeansDao;
import com.juyu.miliao.scheduler.dao.mysql.GameAllRankingListDao;
import com.juyu.miliao.common.domain.BeansGameDTO;
import com.juyu.miliao.scheduler.domin.GameAllRankingList;
import com.juyu.miliao.scheduler.service.BeansGameService;
import com.juyu.miliao.scheduler.service.GameRankingListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 添加账单
 *
 * @author xuanqicheng
 */
@Component
@Slf4j
public class BillDetailBeansMq {


    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private BillDetailBeansDao billDetailBeansDao;

    @Resource(name = "beansGameSanGuoServiceImpl")
    private BeansGameService beansGameSanGuoServiceImpl;

    @Resource(name = "SanGuoGameRankingListServiceImpl")
    private GameRankingListService sanGuoGameRankingListServiceImpl;
    @Resource
    private GameAllRankingListDao gameAllRankingListDao;


    @RabbitListener(bindings = @QueueBinding(value = @Queue(BillDetailBeansMqKey.CreateCommontBillDetailBeansMQKEY.QUEUE_KEY),
            exchange = @Exchange(BillDetailBeansMqKey.EXCHANGE_KEY),
            key = BillDetailBeansMqKey.CreateCommontBillDetailBeansMQKEY.ROUTING_KEY))
    public void getBillDetailBeansMqMessage(String message) {
        if (!StringUtils.isEmpty(message)) {
            try {
                BillDetailBeansDTO billDetailBeansDTO = objectMapper.readValue(message, BillDetailBeansDTO.class);
                BillDetailBeans billDetailBeans = new BillDetailBeans();
                BeanUtils.copyProperties(billDetailBeansDTO, billDetailBeans);
                billDetailBeansDao.insert(billDetailBeans);

            } catch (Exception ex) {
                log.error("处理寻豆账单据错误：" + message + "|erro=" + ex.getMessage());
            }
        }
    }

    /**
     * 游戏寻豆账单队列
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(BillDetailBeansMqKey.CreateGameBillDetailBeansMQKey.QUEUE_KEY),
            exchange = @Exchange(BillDetailBeansMqKey.EXCHANGE_KEY),
            key = BillDetailBeansMqKey.CreateGameBillDetailBeansMQKey.ROUTING_KEY))
    public void getBillDetailBeansGameMqMessage(String message) {
        if (!StringUtils.isEmpty(message)) {
            try {
                BeansGameDTO beansGameDTO = objectMapper.readValue(message, BeansGameDTO.class);
                if (beansGameDTO.getGameType() == 1 || beansGameDTO.getGameType() == 2) {
                    beansGameSanGuoServiceImpl.insertBillBeansGame(beansGameDTO);
                    sanGuoGameRankingListServiceImpl.setRankingList(beansGameDTO);
                }
                //存入总排行榜数据库中
                GameAllRankingList gameAllRankingList = getGameAllRankingList(beansGameDTO);
                GameAllRankingList gameAllRankingList1 = gameAllRankingListDao.findListByUserId(beansGameDTO.getUserId(), LocalDate.now());
                if (gameAllRankingList1 == null) {
                    gameAllRankingListDao.addGameRankingList(gameAllRankingList);
                } else {
                    gameAllRankingListDao.updateGameRankingList(gameAllRankingList);
                }
            } catch (Exception ex) {
                log.error("处理寻豆三国游戏账单入库错误：" + message + "|erro=" + ex.getMessage());
            }
        }
    }

    public GameAllRankingList getGameAllRankingList(BeansGameDTO beansGameDTO) {

        GameAllRankingList gameAllRankingList = new GameAllRankingList();
        gameAllRankingList.setUserId(beansGameDTO.getUserId());
        gameAllRankingList.setConsumeBean(beansGameDTO.getConsume());
        gameAllRankingList.setGameProfit(beansGameDTO.getProfit() + beansGameDTO.getConsume());
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        gameAllRankingList.setCreateTime(LocalDateTime.parse(beansGameDTO.getCreateTime(), timeFormatter));
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        gameAllRankingList.setCreateDate(LocalDate.parse(beansGameDTO.getCreateDate(), dateFormatter));
        gameAllRankingList.setUpdateTime(LocalDateTime.parse(beansGameDTO.getCreateTime(), timeFormatter));
        gameAllRankingList.setReturnBean(beansGameDTO.getProfit());
        return gameAllRankingList;
    }

}
